import axios from 'axios';
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        userid: 0,
        username: '匿名用户',
    },
    mutations: {
        setUserid(state, uid) {
            console.log("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
            console.log(uid)
            state.userid = uid;
        },
        setUsername(state, uname) {
            state.username = uname;
        }
    },
    actions: {
        ulogin(context, obj) {
            console.log("ulogin")
            return new Promise((resolve, reject) => {
                axios.post("http://pw.clianmaimai.com:8071/dn/select_user",
                    `user_name=${obj.username}&user_pwd=${obj.password}`
                ).then(result => {
                    console.log(result.data);
                    if (result.data.length > 0) {
                        context.commit(
                            "setUsername",
                            result.data[0].user_name
                        );
                        context.commit(
                            "setUserid",
                            result.data[0].user_id
                        );
                        resolve();
                    } else {
                        reject(Error);
                    }
                })
            })
        }
    },
    modules: {}
})